#region

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using ThuySan.Bussiness;
using ThuySan.IRepository;

#endregion

namespace ThuySan.Repository
{
    public partial class PropertyRepository : BaseEntityRepository<FishesStoreContext, Property>, IPropertyRepository
    {
        public PropertyRepository(FishesStoreContext context)
        {
            DataContext = context;
        }

        #region Implementation of IBaseRepository<FishesStoreDataContext>

        public void Delete(int value)
        {
            Property temp = SelectbyId(value);
            Delete(temp);
        }

        public void Delete(int value, out Exception exception)
        {
            try
            {
                Delete(value);
                exception = null;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
        }

        public void Delete(IEnumerable<int> value)
        {
            IQueryable<Property> query = DbSet.Where(c => value.Contains(c.PropertyId));
            Delete(query);
        }

        public void Delete(IEnumerable<int> value, out Exception exception)
        {
            try
            {
                Delete(value);
                exception = null;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
        }

        public virtual void Delete(Property value)
        {
            if (DataContext.Entry(value).State == EntityState.Detached)
                DbSet.Attach(value);
            DataContext.Entry(value).State = EntityState.Deleted;
            AcceptChanges();
        }

        public virtual void Delete(Property value, out Exception exception)
        {
            try
            {
                Delete(value);
                exception = null;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
        }

        public void Delete(IEnumerable<Property> value)
        {
            SetEntityState(value, EntityState.Deleted);
            AcceptChanges();
        }

        public void Delete(IEnumerable<Property> value, out Exception exception)
        {
            try
            {
                Delete(value);
                exception = null;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
        }

        public virtual void Insert(Property value)
        {
            DbSet.Add(value);
            AcceptChanges();
        }

        public virtual void Insert(Property value, out Exception exception)
        {
            try
            {
                Insert(value);
                exception = null;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
        }

        public virtual IQueryable<Property> Select()
        {
            return DbSet;
        }

        public virtual IQueryable<Property> Select(out Exception exception)
        {
            try
            {
                IQueryable<Property> query = Select();
                exception = null;
                return query;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
            return null;
        }

        public virtual IQueryable<Property> Select(Expression<Func<Property, bool>> predicate, string include = "")
        {
            IQueryable<Property> query = DbSet;
            if (predicate != null)
                query = query.Where(predicate);
            if (!string.IsNullOrEmpty(include))
            {
                string[] s = include.Split(',');
                query = s.Aggregate(query, (current, s1) => current.Include(s1));
            }
            return query;
        }

        public virtual IQueryable<Property> Select(Expression<Func<Property, bool>> predicate, string include, out Exception exception)
        {
            try
            {
                IQueryable<Property> query = Select(predicate, include);
                exception = null;
                return query;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
            return null;
        }

        public virtual IQueryable<Property> Select<TKey>(Expression<Func<Property, bool>> predicate = null, Expression<Func<Property, TKey>> keySelector = null, ListSortDirection direction = ListSortDirection.Ascending, string include = "")
        {
            IQueryable<Property> query = DbSet;
            if (predicate != null)
                query = query.Where(predicate);
            if (keySelector != null)
                query = direction == ListSortDirection.Descending ? query.OrderByDescending(keySelector) : query.OrderBy(keySelector);
            if (!string.IsNullOrEmpty(include))
            {
                string[] s = include.Split(',');
                query = s.Aggregate(query, (current, s1) => current.Include(s1));
            }
            return query;
        }

        public virtual IQueryable<Property> Select<TKey>(Expression<Func<Property, bool>> predicate, Expression<Func<Property, TKey>> keySelector, ListSortDirection direction, string include, out Exception exception)
        {
            try
            {
                IQueryable<Property> query = Select(predicate, keySelector, direction, include);
                exception = null;
                return query;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
            return null;
        }

        public virtual Property SelectbyId(int value)
        {
            Property temp = DbSet.Find(value);
            return temp;
        }

        public virtual Property SelectbyId(int value, out Exception exception)
        {
            try
            {
                Property temp = SelectbyId(value);
                exception = null;
                return temp;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
            return null;
        }

        public virtual void Update(Property value)
        {
            if (DataContext.Entry(value).State == EntityState.Detached)
                DbSet.Attach(value);
            DataContext.Entry(value).State = EntityState.Modified;
            AcceptChanges();
        }

        public virtual void Update(Property value, out Exception exception)
        {
            try
            {
                Update(value);
                exception = null;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
        }

        #endregion
    }
}
